package com.esri.android.map.ogc;

import android.util.Log;
import com.esri.android.map.DynamicLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.geometry.Envelope;
import com.esri.core.geometry.SpatialReference;
import com.esri.core.internal.tasks.e.a.c;
import com.esri.core.internal.tasks.e.a.d;
import com.esri.core.internal.tasks.e.a.e;
import com.esri.core.internal.tasks.e.a.f;
import com.esri.core.internal.tasks.e.a.g;
import com.esri.core.internal.tasks.e.a.h;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.RejectedExecutionException;

/* loaded from: classes.dex */
public class WMSLayer extends DynamicLayer {
    private boolean A;
    private boolean B;
    private String C;
    private String[] D;
    private List<WMSLayerInfo> s;
    private Set<String> t;
    private String u;
    private String[] v;
    private boolean w;
    private String[] x;
    private String y;
    private String z;

    public WMSLayer(String str) {
        this(str, null, true, null, null, true);
    }

    public WMSLayer(String str, String[] strArr) {
        this(str, null, true, strArr, null, true);
    }

    public WMSLayer(String str, SpatialReference spatialReference) {
        this(str, spatialReference, true, null, null, true);
    }

    public WMSLayer(String str, boolean z) {
        this(str, null, z, null, null, true);
    }

    public WMSLayer(String str, SpatialReference spatialReference, boolean z, String[] strArr, UserCredentials userCredentials, boolean z2) {
        super(str);
        this.t = new LinkedHashSet();
        this.v = null;
        this.w = true;
        this.x = null;
        this.y = "image/png";
        this.z = null;
        this.A = true;
        this.B = false;
        this.C = null;
        this.D = null;
        setCredentials(userCredentials);
        this.v = strArr;
        this.A = z;
        setDefaultSpatialReference(spatialReference);
        if (spatialReference != null) {
            this.B = true;
        }
        if (z2) {
            try {
                getPoolExecutor().submit(new Runnable() { // from class: com.esri.android.map.ogc.WMSLayer.1
                    @Override // java.lang.Runnable
                    public void run() {
                        WMSLayer.this.initLayer();
                    }
                });
            } catch (RejectedExecutionException unused) {
            }
        }
    }

    public WMSLayerInfo[] getLayers() {
        List<WMSLayerInfo> list = this.s;
        if (list == null || list.size() == 0) {
            return null;
        }
        return (WMSLayerInfo[]) this.s.toArray(new WMSLayerInfo[0]);
    }

    public String[] getVisibleLayers() {
        return this.v;
    }

    public void setVisibleLayer(String[] strArr) {
        this.v = strArr;
    }

    public String[] getSupportedImageFormats() {
        return this.D;
    }

    public SpatialReference[] getSupportedSpatialReferences() {
        return this.supportedSRs;
    }

    @Override // com.esri.android.map.DynamicLayer
    protected byte[] getImage(int i, int i2, Envelope envelope) throws Exception {
        String version = getVersion();
        String[] strArr = this.v;
        if (strArr == null) {
            strArr = this.x;
        }
        String[] strArr2 = strArr;
        String str = this.u;
        String str2 = this.y;
        if (str2 == null) {
            str2 = null;
        }
        return new d(new e(version, strArr2, str, envelope, i, i2, str2, this.w), this.C, this.credentials).execute();
    }

    public void setImageFormat(String str) {
        this.y = str;
    }

    public boolean isTransparent() {
        return this.w;
    }

    public void setTransparent(boolean z) {
        this.w = z;
    }

    public String getImageFormat() {
        return this.y;
    }

    public String getVersion() {
        return this.z;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.Layer
    public void updateSpatialReferenceStatus(int i) {
        Set<String> set;
        if (!this.B && i == 2 && (set = this.t) != null) {
            boolean z = false;
            for (String str : set) {
                int b = g.b(str);
                int latestID = getSpatialReference().getLatestID();
                if (latestID == 3857) {
                    if (b == 3857 || b == 3785 || b == 102100 || b == 102113) {
                        this.u = str;
                        z = true;
                        break;
                    }
                } else if (b == latestID) {
                    this.u = str;
                    z = true;
                    break;
                }
            }
            if (!z) {
                this.u = g.a(getSpatialReference(), (List<String>) null);
            }
        }
        super.updateSpatialReferenceStatus(i);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // com.esri.android.map.DynamicLayer, com.esri.android.map.Layer
    public void initLayer() {
        int[] a;
        if (this.nativeHandle == 0) {
            this.nativeHandle = create();
        }
        if (this.nativeHandle == 0) {
            changeStatus(OnStatusChangedListener.STATUS.fromInt(-1000));
            Log.e(com.esri.core.internal.a.a, "initialize fail : " + OnStatusChangedListener.STATUS.fromInt(-1000));
            return;
        }
        try {
            SpatialReference defaultSpatialReference = getDefaultSpatialReference();
            if (defaultSpatialReference != null && defaultSpatialReference.getID() == -1) {
                throw new RuntimeException("The Spatial reference has to have a valid id");
            }
            f execute = this.serviceInfo != null ? (f) this.serviceInfo : new com.esri.core.internal.tasks.e.a.b(new c(), getUrl(), this.credentials).execute();
            this.z = execute.f();
            String a2 = execute.a();
            this.C = a2;
            if (a2 == null) {
                this.C = getUrl();
            }
            this.t.addAll(execute.d());
            this.D = (String[]) execute.l().toArray(new String[0]);
            if (!this.B) {
                setDefaultSpatialReference(execute.e());
                setFullExtent(execute.c());
                setInitialExtent(execute.c());
                this.u = execute.b().b();
            } else {
                this.u = g.a(getDefaultSpatialReference(), execute.d());
                Envelope a3 = execute.a(execute.j().get(0), this.u, getDefaultSpatialReference(), this.z);
                setFullExtent(a3);
                setInitialExtent(a3);
            }
            if (this.A && (a = g.a(this.t)) != null && a.length > 1) {
                this.supportedSRs = new SpatialReference[a.length];
                for (int i = 0; i < a.length; i++) {
                    this.supportedSRs[i] = SpatialReference.create(a[i]);
                }
            }
            if (execute.k() != null) {
                if (execute.k().size() > 0) {
                    this.s = new ArrayList();
                    for (h hVar : execute.k()) {
                        WMSLayerInfo wMSLayerInfo = new WMSLayerInfo();
                        wMSLayerInfo.a(hVar.b());
                        wMSLayerInfo.b(hVar.c());
                        wMSLayerInfo.c(hVar.d());
                        this.s.add(wMSLayerInfo);
                    }
                }
            } else {
                for (h hVar2 : execute.j()) {
                    if (this.s == null) {
                        this.s = new LinkedList();
                    }
                    this.s.addAll(a(hVar2));
                }
            }
            List<WMSLayerInfo> list = this.s;
            if (list != null && list.size() > 0) {
                ArrayList arrayList = new ArrayList();
                for (WMSLayerInfo wMSLayerInfo2 : this.s) {
                    if (wMSLayerInfo2.getName() != null) {
                        arrayList.add(wMSLayerInfo2.getName());
                    }
                }
                this.x = (String[]) arrayList.toArray(new String[0]);
            }
            super.initLayer();
        } catch (Exception e) {
            Log.e(com.esri.core.internal.a.a, "url =" + getUrl(), e);
            if (e instanceof EsriSecurityException) {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(((EsriSecurityException) e).getCode()));
            } else {
                changeStatus(OnStatusChangedListener.STATUS.fromInt(OnStatusChangedListener.EsriStatusException.INIT_FAILED_ARCGIS_DYNAMIC_LAYER));
            }
        }
    }

    private List<WMSLayerInfo> a(h hVar) {
        if (hVar == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        LinkedList linkedList = new LinkedList();
        LinkedList linkedList2 = new LinkedList();
        linkedList.add(hVar);
        linkedList2.add(new WMSLayerInfo());
        while (true) {
            h hVar2 = (h) linkedList.poll();
            if (hVar2 == null) {
                return arrayList;
            }
            WMSLayerInfo wMSLayerInfo = (WMSLayerInfo) linkedList2.poll();
            wMSLayerInfo.a(hVar2.b());
            wMSLayerInfo.b(hVar2.c());
            wMSLayerInfo.c(hVar2.d());
            wMSLayerInfo.a(hVar2.e());
            wMSLayerInfo.a(hVar2.f());
            if (hVar2.j() != null && hVar2.j().size() > 0) {
                ArrayList arrayList2 = new ArrayList();
                for (h hVar3 : hVar2.j()) {
                    WMSLayerInfo wMSLayerInfo2 = new WMSLayerInfo();
                    arrayList2.add(wMSLayerInfo2);
                    linkedList.add(hVar3);
                    linkedList2.add(wMSLayerInfo2);
                }
                wMSLayerInfo.a(arrayList2);
            }
            arrayList.add(wMSLayerInfo);
        }
    }
}
